package logic.blocks;

import java.awt.Rectangle;
import java.awt.geom.Line2D;

import logic.characters.Player;
import logic.enemies.Enemy;
import logic.entities.Background;
import logic.entities.GameObject;
import logic.pruebas.GamePanel;

public abstract class Block extends GameObject {
	private static final long serialVersionUID = -5238460621756481007L;
	public boolean playerOnThis = false;
	public boolean enemyOnThis = false;
	public boolean relevant = true;
	protected float velX;
	protected float velY;
	@SuppressWarnings("unused")
	private Player player;
	private float initXCoord;
	private float initYCoord;

	public Block(float x, float y, GamePanel panel, String nomImg, Player player) {
		super(x, y, panel, nomImg);
		this.player = player;
		dead = false;
		initXCoord = coorX;
		initYCoord = coorY;
		collisionX = coorX;
		collisionY = coorY;
		collisionWidth = anchura;
		collisionHeight = altura;
	}

	public void impactoJugador(GameObject object) {
		if (dibujado) {
			if (object.dibujado && !object.dead) {
					Line2D pUp = new Line2D.Float(object.collisionX + 5, object.collisionY, object.collisionX + object.collisionWidth - 7, object.collisionY);
					Line2D pDown = new Line2D.Float(object.collisionX + 5, object.collisionY + object.collisionHeight, object.collisionX + object.collisionWidth - 7, object.collisionY + object.collisionHeight);
					Line2D pLeft = new Line2D.Float(object.collisionX, object.collisionY + 5, object.collisionX, object.collisionY + object.collisionHeight - 7);
					Line2D pRight = new Line2D.Float(object.collisionX + object.collisionWidth, object.collisionY + 5, object.collisionX + object.collisionWidth, object.collisionY + object.collisionHeight - 7);
					Rectangle thisObject = new Rectangle((int)coorX, (int)coorY, anchura, altura);

					if (pDown.intersects(thisObject)) {
						if (object instanceof Player) {
							Player tempP = (Player) object;
							downImpact(tempP);
						} else if (object instanceof Enemy) {
							Enemy tempE = (Enemy) object;
							downImpact(tempE);
						}
//						object.velY = 0;
//						object.coorY = coorY - object.altura - 1;
					} else if (pUp.intersects(thisObject)) {
						if (object instanceof Player) {
							Player tempP = (Player) object;
							upImpact(tempP);
						} else if (object instanceof Enemy) {
							Enemy tempE = (Enemy) object;
							upImpact(tempE);
						}
					} else if (pLeft.intersects(thisObject)) {
						if (object instanceof Player) {
							Player tempP = (Player) object;
							leftImpact(tempP);
						} else if (object instanceof Enemy) {
							Enemy tempE = (Enemy) object;
							leftImpact(tempE);
						}
					} else if (pRight.intersects(thisObject)) {
						if (object instanceof Player) {
							Player tempP = (Player) object;
							rightImpact(tempP);
						} else if (object instanceof Enemy) {
							Enemy tempE = (Enemy) object;
							rightImpact(tempE);
						}
					} else {
						if (object instanceof Player) {
							if (playerOnThis) {
							Player tempP = (Player) object;
							if (!tempP.attacking && !tempP.special && !tempP.throwing) {
								if ((object.collisionX + object.collisionWidth < coorX) || (object.collisionX > coorX + anchura)) {
									endCollision(tempP);
								}
							}
							}

						} else if (object instanceof Enemy) {
							if (enemyOnThis) {
								Enemy tempE = (Enemy) object;
							if ((tempE.collisionX + tempE.collisionWidth < collisionX) || (tempE.collisionX > coorX + anchura) ) {
								endCollision(tempE);
								}
							}
						}
					}
			}
		}
	}
	
	public void endCollision(Player player) {
		player.onPlatform = false;
		playerOnThis = false;
	}
	
	public void endCollision(Enemy enemy) {
		enemy.onPlatform = false;
		enemy.falling = true;
		enemyOnThis = false;
	}


			//	public void update() {
//		if (this.dibujado) {
//			if (player.coorX > 400f && player.movingRight) {
//				velX = -200;
//			} else if (player.coorX < 400f && player.movingLeft){
//				velX = 200;
//			} else {
//				velX = 0;
//			}
//			float tiempoTranscurridoEnSegundos = 
//					(System.currentTimeMillis() - ultimoDibujo) / 1000.0F;
//			if (tiempoTranscurridoEnSegundos > 0.01F) {
//				float antCoordX = coorX;
//				coorX = coorX+velX * tiempoTranscurridoEnSegundos;
//				coorY = coorY+velY * tiempoTranscurridoEnSegundos;
//				if (seSaleX()) {
//					velX = 0;
//					coorX = antCoordX;
//				}
//			}
//			dibujar();
//		}
//	}	

//	public boolean seSaleX() {
//		return (coorX < initXCoord - 224 || coorX > initXCoord + 224);
//	}
	
	public void update(Background background) {
//		if (this.coorX < player.coorX - 900f || this.coorX > player.coorX + 900f || this.coorY < player.coorY - 700f || this.coorY > player.coorY + 700f) {
		//			relevant = false;
		//		} else {
		//			relevant = true;
		//		}
//		if (seSaleX() || seSaleY()) {
//			relevant = false;
//			panelJuego.remove(this);
//			dibujado = false;
//		} else {
//			relevant = true;
//			panelJuego.add(this);
//		}
		coorX = background.coorX + initXCoord;
		coorY = background.coorY + initYCoord;
		collisionX = coorX;
		collisionY = coorY;
			//if (relevant) {
				dibujar();
		//}
	}

	public void checkRelevant(Player player) {
		if (this.coorX < player.coorX - 900f || this.coorX > player.coorX + 900f || this.coorY < player.coorY - 700f || this.coorY > player.coorY + 700f) {
			relevant = false;
		} else {
			relevant = true;
		}
	}
	
	public abstract void upImpact(Player player);
	public abstract void downImpact(Player player);
	public abstract void leftImpact(Player player);
	public abstract void rightImpact(Player player);
	
	public abstract void upImpact(Enemy enemy);
	public abstract void downImpact(Enemy enemy);
	public abstract void leftImpact(Enemy enemy);
	public abstract void rightImpact(Enemy enemy);

}
